# include "V_LD1.h"

uint8_t V_LD1_RxBit=0;
uint8_t V_LD1_RxBuffer[1024]={0};
uint8_t V_LD1_RxFlag=0;
uint8_t V_LD1_Status=0;

V_LD1_Radar_Parameter_Struct V_LD1_Radar_Parameter_Global={0};

V_LD1_Struct Read_V_LD1_Stu(void)
{
	V_LD1_Struct V_LD1_Stu;
	memset(&V_LD1_Stu,0,sizeof(V_LD1_Stu));
	uint8_t i=0;
	while(V_LD1_Status<2)
	{
		i++;
		delay_ms(10);
		if(i>=50)
		{
			V_LD1_RxBit=0;
			V_LD1_Status=0;
			V_LD1_RxFlag=0;
			return V_LD1_Stu;
		}
	}
	
	memcpy(&V_LD1_Stu.Header[0],&V_LD1_RxBuffer[0],4);
	memcpy(&V_LD1_Stu.Length,&V_LD1_RxBuffer[4],4);
	memcpy(&V_LD1_Stu.DATA[0],&V_LD1_RxBuffer[8],V_LD1_Stu.Length);
	V_LD1_RxBit=0;
	V_LD1_Status=0;
	V_LD1_RxFlag=0;
	
	return V_LD1_Stu;
}

void Send_V_LD1_Stu(V_LD1_Struct V_LD1_Stu)
{
	uint8_t buf[51]={0};
	memcpy(buf,&V_LD1_Stu,V_LD1_Stu.Length+8);
	V_LD1_RxBit=0;
	V_LD1_Status=0;
	V_LD1_RxFlag=0;
	HAL_UART_Transmit(&V_LD1_UART_Handle,buf,V_LD1_Stu.Length+8,0xFFFF);
}

int Read_V_LD1_RESP(void)
{
	V_LD1_Struct V_LD1_Stu=Read_V_LD1_Stu();
	if (V_LD1_Stu.Header[0]=='R' && V_LD1_Stu.Header[1]=='E' && V_LD1_Stu.Header[2]=='S' && V_LD1_Stu.Header[3]=='P' && V_LD1_Stu.Length==1)
	{
		return V_LD1_Stu.DATA[0];
	}
	else
	{
		return -1;
	}
}

int Read_V_LD1_VERS(V_LD1_Struct* V_LD1)
{
	V_LD1_Struct V_LD1_Stu=Read_V_LD1_Stu();
	if (V_LD1_Stu.Header[0]=='V' && V_LD1_Stu.Header[1]=='E' && V_LD1_Stu.Header[2]=='R' && V_LD1_Stu.Header[3]=='S' && V_LD1_Stu.Length==19)
	{
		memcpy(V_LD1,&V_LD1_Stu,27);
		return 0;
	}
	else
	{
		return -1;
	}
}

void Read_V_LD1_Radar(void)
{
	V_LD1_Struct V_LD1_Stu={0};
	V_LD1_PDAT_Struct PDAT_Stu = {0};
	GUI_Struct Stu={0};
	uint8_t RESP_Code=0;
	
	memcpy(&V_LD1_Stu.Header[0],"GNFD",4);
	V_LD1_Stu.Length=1;
	V_LD1_Stu.DATA[0]=0|V_LD1_GNFD_PDAT;	
	Send_V_LD1_Stu(V_LD1_Stu);
	RESP_Code=Read_V_LD1_RESP();
	printf("[INFO] GNFD PDAT RESP: %d\n",RESP_Code);
	
	V_LD1_Stu=Read_V_LD1_Stu();
	
	if (V_LD1_Stu.Header[0]=='P' && V_LD1_Stu.Header[1]=='D' && V_LD1_Stu.Header[2]=='A' && V_LD1_Stu.Header[3]=='T' && V_LD1_Stu.Length==6)
	{
		memcpy(&PDAT_Stu,&V_LD1_Stu.DATA[0],6);
		printf("[INFO] PDAT: %f %d\n",PDAT_Stu.Distance,PDAT_Stu.Magnitude_Of_Target);
		Stu.COM=0x00;
		Stu.BCNT[0]=0;
		Stu.BCNT[1]=6;
		memcpy(&Stu.DATA[0],&PDAT_Stu,6);
		GUI_Slave_Send(Stu);
	}
}

void Init_Radar_Parameter(void)
{
	V_LD1_Radar_Parameter_Global.Distance_Range=0;
	V_LD1_Radar_Parameter_Global.Threshold_Offset=60;
	V_LD1_Radar_Parameter_Global.Min_Range_Filter=5;
	V_LD1_Radar_Parameter_Global.Max_Range_Filter=460;
	V_LD1_Radar_Parameter_Global.Distance_Average_Count=5;
	V_LD1_Radar_Parameter_Global.Target_Filter=0;
	V_LD1_Radar_Parameter_Global.Distance_Precision=1;
	V_LD1_Radar_Parameter_Global.TX_Power=31;
	V_LD1_Radar_Parameter_Global.Chirp_Integration_Count=20;
	V_LD1_Radar_Parameter_Global.Short_Range_Distance_Filter=0;
}

void Init_V_LD1(void)
{
	V_LD1_Struct V_LD1_Stu={0};
	V_LD1_RxBit=0;
	V_LD1_Status=0;
	V_LD1_RxFlag=0;
	
	uint8_t RESP_Code=0;
	memset(V_LD1_RxBuffer,0,sizeof(V_LD1_RxBuffer));
	
	memcpy(&V_LD1_Stu.Header[0],"RFSE",4);
	V_LD1_Stu.Length=0;
	Send_V_LD1_Stu(V_LD1_Stu);
	RESP_Code=Read_V_LD1_RESP();
	printf("[INFO] RFSE RESP: %d\n",RESP_Code);
	
	memcpy(&V_LD1_Stu.Header[0],"INIT",4);
	V_LD1_Stu.Length=1;
	V_LD1_Stu.DATA[0]=0;	
	Send_V_LD1_Stu(V_LD1_Stu);
	RESP_Code=Read_V_LD1_RESP();
	printf("[INFO] INIT RESP: %d\n",RESP_Code);
	
	if(RESP_Code==V_LD1_RESP_OK)
	{
		if(Read_V_LD1_VERS(&V_LD1_Stu)==0)
		{
			printf("[INFO] V_LD1_Version: %s\n",V_LD1_Stu.DATA);
			memcpy(&V_LD1_Radar_Parameter_Global.Version[0],&V_LD1_Stu.DATA[0],19);
		}
	}
	
	memcpy(&V_LD1_Stu.Header[0],"TGFI",4);
	V_LD1_Stu.Length=1;	
	V_LD1_Stu.DATA[0]=0	;
	Send_V_LD1_Stu(V_LD1_Stu);
	RESP_Code=Read_V_LD1_RESP();
	printf("[INFO] TGFI RESP: %d\n",RESP_Code);
	
	memcpy(&V_LD1_Stu.Header[0],"INTN",4);
	V_LD1_Stu.Length=1;	
	V_LD1_Stu.DATA[0]=20;
	Send_V_LD1_Stu(V_LD1_Stu);
	RESP_Code=Read_V_LD1_RESP();
	printf("[INFO] INTN RESP: %d\n",RESP_Code);
	
	memcpy(&V_LD1_Stu.Header[0],"SRDF",4);
	V_LD1_Stu.Length=1;	
	V_LD1_Stu.DATA[0]=0;
	Send_V_LD1_Stu(V_LD1_Stu);
	RESP_Code=Read_V_LD1_RESP();
	printf("[INFO] SRDF RESP: %d\n",RESP_Code);
	
	Read_V_LD1_Radar();	
}

